package flc.ast.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.reflect.TypeToken;
import com.youth.banner.config.IndicatorConfig;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.AllRecordActivity;
import flc.ast.activity.PlayLocalActivity;
import flc.ast.activity.PlayUriActivity;
import flc.ast.activity.ProMusicActivity;
import flc.ast.activity.ProSelVideoActivity;
import flc.ast.activity.SearchDeviceActivity;
import flc.ast.activity.SelPictureActivity;
import flc.ast.activity.SelVideoActivity;
import flc.ast.adapter.MyBannerAdapter;
import flc.ast.adapter.RecordAdapter;
import flc.ast.bean.RecordBean;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.SPUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private List<RecordBean> listShow = new ArrayList<>();
    private RecordAdapter recordAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);
    }

    @Override
    protected void initData() {
        List<Integer> listImg = new ArrayList<>();
        listImg.add(R.drawable.kp_sptp);
        listImg.add(R.drawable.kp_yptp);
        listImg.add(R.drawable.kp_tptp);
        MyBannerAdapter bannerAdapter = new MyBannerAdapter(listImg);
        mDataBinding.homeBanner.setAdapter(bannerAdapter, true);//false不无限循环
        mDataBinding.homeBanner.setBannerGalleryEffect(30, 10);//40两边露出距离，20图片之间距离
        mDataBinding.homeBanner.isAutoLoop(true);//false不自动轮播
        //点击监听
        mDataBinding.homeBanner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(Object data, int position) {
                if (position == 0) {
                    gotoPro(ProSelVideoActivity.class);
                } else if (position == 1) {
                    gotoPro(ProMusicActivity.class);
                } else if (position == 2) {
                    gotoPro(SelPictureActivity.class);
                }
            }
        });
        mDataBinding.homeBanner.setIndicator(new CircleIndicator(getActivity()));
        mDataBinding.homeBanner.setIndicatorNormalColor(Color.parseColor("#1A081935"));
        mDataBinding.homeBanner.setIndicatorSelectedColor(Color.parseColor("#FFB999FE"));
        mDataBinding.homeBanner.setIndicatorGravity(IndicatorConfig.Direction.CENTER);
        mDataBinding.rvRecordList.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        recordAdapter = new RecordAdapter();
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
        mDataBinding.ivProConn.setOnClickListener(this);
        mDataBinding.ivPlayUri.setOnClickListener(this);
        mDataBinding.ivPlayFormat.setOnClickListener(this);
        mDataBinding.ivPlayLocal.setOnClickListener(this);
        mDataBinding.tvAllRecord.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivProConn:
                startActivity(SearchDeviceActivity.class);
                break;
            case R.id.ivPlayUri:
                startActivity(PlayUriActivity.class);
                break;
            case R.id.ivPlayFormat:
                startActivity(SelVideoActivity.class);
                break;
            case R.id.ivPlayLocal:
                startActivity(PlayLocalActivity.class);
                break;
            case R.id.tvAllRecord:
                startActivity(AllRecordActivity.class);
                break;
        }
    }

    //去到投屏界面
    private void gotoPro(Class cls) {
        if (!Content.IsConnect) {
            ToastUtils.showShort(R.string.please_conn_pro_device);
            return;
        }
        startActivity(cls);
    }

    @Override
    public void onResume() {
        super.onResume();
        isConnect();
        listShow.clear();
        List<RecordBean> listSp = AppSpUtils.getRecordList();
        if (listSp != null && listSp.size() > 0) {
            if (listSp.size() > 3) {
                listShow = listSp.subList(0, 3);
            } else {
                listShow.addAll(listSp);
            }
            recordAdapter.setList(listShow);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
            mDataBinding.llPlayRecordNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
            mDataBinding.llPlayRecordNoData.setVisibility(View.VISIBLE);
        }
    }

    private void isConnect() {
        if (Content.IsConnect) {
            mDataBinding.tvProName.setText(getString(R.string.pro_conn) + Content.ConnectName);
            mDataBinding.tvProName.setTextColor(Color.parseColor("#FF081935"));
        } else {
            mDataBinding.tvProName.setText(getString(R.string.not_conn));
            mDataBinding.tvProName.setTextColor(Color.parseColor("#4D081935"));
        }
    }

}